package com.zzj.security.jwt.config.security.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.session.HttpSessionEventPublisher;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;

/**
 * 监听 HttpSession 创建与销毁
 */
@Component
public class CustomizeHttpSessionListener extends HttpSessionEventPublisher {

    private static final Logger logger = LoggerFactory.getLogger(CustomizeHttpSessionListener.class);

    @Autowired
    private SessionRegistry sessionRegistry;

    @Override
    public void sessionCreated(HttpSessionEvent event) {

        super.sessionCreated(event);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        logger.info("{}.sessionDestroyed() --> 销毁Session: {}, {}",this.getClass().getName(), session.getId(), sessionRegistry.getAllPrincipals().size());
        super.sessionDestroyed(event);
    }
}
